package com.blacksheep.mtm.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.pojo.manager.TbDevice;
import com.blacksheep.mtm.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api("设备管理列表")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @ResponseBody
    @RequestMapping(value = "/device/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取订单列表")
    public DataTablesResult getdeviceList(int draw, int start, int length, String searchKey, int[] state,
                                            String minDate, String maxDate,String[] type,
                                            @RequestParam("search[value]") String search,
                                            @RequestParam("order[0][column]") int orderCol,
                                            @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "type","code", "create_time","state"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= deviceService.getDeviceList(draw,start,length,state,type,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "/device/ws/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取温湿度计列表")
    public DataTablesResult getWSDeviceList(int draw, int start, int length, String searchKey, int[] state,
                                          String minDate, String maxDate,
                                          @RequestParam("search[value]") String search,
                                          @RequestParam("order[0][column]") int orderCol,
                                          @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","id", "type","code", "remark", "create_time","state"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= deviceService.getWSDeviceList(draw,start,length,state,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "/device/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增设备")
    public Result<TbDevice> insert(TbDevice device){
        TbDevice result = deviceService.insert(device);
        return new ResultUtil<TbDevice>().setData(result);
    }
    @ResponseBody
    @RequestMapping(value = "/device/add/batch",method = RequestMethod.POST)
    @ApiOperation(value = "新增设备")
    public Result<TbDevice> insertBatch(TbDevice device,int num){
        TbDevice result = deviceService.insertBatch(device,num);
        return new ResultUtil<TbDevice>().setData(result);
    }
    @ResponseBody
    @RequestMapping(value = "/device/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新")
    public Result<TbDevice> update(TbDevice device){
        deviceService.updateDevice(device);
        return new ResultUtil<TbDevice>().setData(null);
    }
    @ResponseBody
    @RequestMapping(value = "/device/stop/{id}",method = RequestMethod.PUT)
    @ApiOperation(value = "下架")
    public Result<TbDevice> stop(@PathVariable Integer id){
        TbDevice device = deviceService.alertState(id,0);
        return new ResultUtil<TbDevice>().setData(device);
    }
    @ResponseBody
    @RequestMapping(value = "/device/start/{id}",method = RequestMethod.PUT)
    @ApiOperation(value = "上架")
    public Result<TbDevice> start(@PathVariable Integer id){
        TbDevice device = deviceService.alertState(id,1);
        return new ResultUtil<TbDevice>().setData(device);
    }

    @RequestMapping(value = "/device/destroy/{id}",method = RequestMethod.PUT)
    @ApiOperation(value = "上架")
    @ResponseBody
    public Result<TbDevice> destroyDevice(@PathVariable Integer id){
        TbDevice device = deviceService.alertState(id,-1);
        return new ResultUtil<TbDevice>().setData(device);
    }

    @RequestMapping(value = "/device/type/count",method = RequestMethod.GET)
    @ResponseBody
    public Result<Integer> getDeviceCountByType(String type){
        int count =   deviceService.getDeviceCountByType(type);
        return new ResultUtil<Integer>().setData(count);
    }
    @RequestMapping(value = "/device/box",method = RequestMethod.GET)
    @ResponseBody
    public Result<List<String>> getIceBoxByCode(String code){
        List<String> result = deviceService.getIceBoxList(code);
        return new ResultUtil< List<String>>().setData(result);
    }


    @RequestMapping(value = "/device/download",method = RequestMethod.GET)
    public void download(HttpServletResponse response) throws IOException {

        deviceService.download(response);

    }
}
